package com.eqdd.yiqidian.ui.discover.workcircle;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.ZanListActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.WorkCircleBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.IMUtil;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

public class ZanListActivity extends BaseActivity {
    private ZanListActivityCustom dataBinding;
    private String id;
    private MVCSwipeRefreshHelper<List<WorkCircleBean.UserZanBean>> mvcHelper;
    private SlimAdapterEx<WorkCircleBean.UserZanBean> slimAdapterEx;
    private String idName;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_zan_list);
        initTopTitleBar(View.VISIBLE, "点赞人");
    }

    @Override
    public void initData() {
        id = getIntent().getStringExtra("id");
        idName = getIntent().getStringExtra("idName");
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_26, new SlimInjector<WorkCircleBean.UserZanBean>() {
            @Override
            public void onInject(WorkCircleBean.UserZanBean data, IViewInjector injector) {
                injector.text(R.id.tv_upper_content,data.getName())
                        .text(R.id.tv_down_content,data.getDepartment()+" "+data.getPost())
                        .with(R.id.iv_head,v-> ImageUtil.setCircleImage(data.getHeadImage(), (ImageView) v));
            }
        }).attachTo(dataBinding.recyclerView);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> IMUtil.startPrivate(ZanListActivity.this,slimAdapterEx.getDataItem(position).getGuid(),slimAdapterEx.getDataItem(position).getNickName(),slimAdapterEx.getDataItem(position).getHeadImage()));
        mvcHelper = new MVCSwipeRefreshHelper<>(dataBinding.swipeRefresh);
        ModelRx2DataSource<WorkCircleBean.UserZanBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                return OkGo.<HttpResult<List<WorkCircleBean.UserZanBean>>>post(HttpConfig.BASE_URL + (idName.equals("workCircleId")?HttpConfig.WORK_CIRCLE_ZAN_LIST:HttpConfig.FRIEND_CIRCLE_ZAN_LIST))
                        .params(idName, id)
                        .params("id", page == 1 ? 0 : slimAdapterEx.getDataItem(slimAdapterEx.getData().size() - 1).getId())
                        .converter(new JsonConverter<HttpResult<List<WorkCircleBean.UserZanBean>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        })
                        .adapt(new FlowableBody<HttpResult<List<WorkCircleBean.UserZanBean>>>())
                        .flatMap(new Function<HttpResult<List<WorkCircleBean.UserZanBean>>, Publisher<List<WorkCircleBean.UserZanBean>>>() {
                            @Override
                            public Publisher<List<WorkCircleBean.UserZanBean>> apply(@NonNull HttpResult<List<WorkCircleBean.UserZanBean>> listHttpResult) throws Exception {
                                return Flowable.just(listHttpResult.getItems() == null ? new ArrayList<WorkCircleBean.UserZanBean>() : listHttpResult.getItems());
                            }
                        });

            }
        },20);
        slimAdapterEx.updateData(new ArrayList<>());
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
